<?php 
/**
 *三国夫人静态设置
 */
class App_kingdomslady{
	/**
	 * 三国夫人数据初始化
	 * @param <int> $uin
	 * @param <boolean> $insert 是否在不存在时，新写入数据
	 */
	public static function getAddKingdoms($uin , $insert = true){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_lady` WHERE `uin` = " . $uin);
		
		// 如果数据不存在，则新写入一条数据
		if($insert && $result === false) {
			$query_data = array(
				'uin' => $uin,
				'ap' => 60,
				'hp' => 70,
			);
			$Db->doInsert('app_kingdoms_lady' , $query_data);
			
			$result = $query_data;
		}
		return $result;
	}
	/**
	 * 修改拜师状态
	 * @param <int> $uin
	 * @param <int> $chef
	 */
	public static function getAlterTeacher($uin , $chef){
	
		$Db=loadDB();
		$update_data = array(
			'chef' => $chef,
		);
		
		$success = $Db->doUpdate('app_kingdoms_lady' , $update_data , '`uin`= '.$uin);
		
		return $success ? 0 : 770;
	}
	/**
	 * 修改攻击属性
	 */
	public static function getAlterAP($uin , $ap){
		$Db=loadDB();
		$update_data = array(
			'ap' => $ap,
		);
		$success = $Db->doUpdate('app_kingdoms_lady' , $update_data , '`uin`= '.$uin);
		return $success ? 0 : 777;
	}
	/**
	 * 查询全部武功秘籍
	 */
	public static function getDanceStudio(){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_dance_studio`");
		return $result;
	}
	/**
	 * 查询武功秘籍详细信息
	 * @param <int> $id
	 */
	public static function getOneDanceStudio($id){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_dance_studio` WHERE id = {$id}");
		
		return $result;
	}
	/**
	 * 查询个人已学秘籍,可翻页
	 * @param <int> $uin
	 * @param <int> $pageSize
	 * @param <int> $pageCount
	 */
	public static function getSkill($uin , $pageSize , $pageCount){
		$Db = loadDB();
		$pageSize=($pageSize-1)*$pageCount;
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_dance_studio` , `app_kingdoms_skill` WHERE app_kingdoms_dance_studio.id = app_kingdoms_skill.dance_studio_id AND app_kingdoms_skill.uin = {$uin} LIMIT {$pageSize} , {$pageCount}");
		return $result;
	}
	/**
	 * 查询个人已学秘籍,无翻页
	 * @param <int> $uin
	 */
	public static function getAllSkill($uin){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_skill` WHERE app_kingdoms_skill.uin = {$uin}");
		return $result;
	}
	/**
	 * 获取个人已学秘籍数量
	 * @param <int> $uin
	 */
	public static function getSkillCount($uin , $first_time , $last_time){
		$Db = loadDB();
		$result= $Db->getOne("SELECT COUNT(*) AS `count` FROM `app_kingdoms_skill` WHERE (`time` BETWEEN $first_time and $last_time) AND `uin` = {$uin}");
		return $result;
	}
	/**
	 * 获取已学习秘籍详细资料
	 * @param $uin
	 * @param $id
	 */
	public static function getOneSkill($uin , $id){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_skill` WHERE `uin` = {$uin} AND `dance_studio_id` = {$id}");
		return $result;
	}
	/**
	 * 写入个人秘籍表
	 * @param $uin
	 * @param $id
	 * @param $type
	 * @param $time
	 */
	public static function getAddSkill($uin , $id , $time){
		$Db = loadDB();
		
		//把数据写入数组
		$insert_date = array(
			'uin' => $uin,
			'dance_studio_id' => $id,
			'time' => $time,
		);
		$success = $Db->doInsert('app_kingdoms_skill' , $insert_date);
		return $success ? 0 : 771;
	}
	/**
	 * 写入个人状态表
	 * @param $uin
	 * @param $id
	 * @param $type
	 * @param $time
	 */
	public static function getAddState($uin , $id , $type , $time){
		$Db = loadDB();
		$is_time = time();
		//把数据写入数组
		$insert_date = array(
			'uin' => $uin,
			'object_id' => $id,
			'reply' => '',
			'time' => $is_time,
			'end_time' => $time,
			'type' => $type,
		);
		$success = $Db->doInsert('app_kingdoms_state' , $insert_date);
		return $success ? 0 : 772;
	}
	/**
	 * 获取个人状态表
	 * @param $uin
	 * @param $type
	 * @param $id
	 */
	public static function getOneState($uin , $type , $id){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_state` WHERE `uin` = {$uin} AND `object_id` = {$id} AND `type` = '{$type}'");
		
		return $result;
	}
	/**
	 * 获取状态表
	 * @param $uin
	 * @param $type
	 * @param $id
	 */
	public static function getAllState($uin , $type){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_state` WHERE app_kingdoms_state.uin = {$uin} AND `type` = '{$type}'");

		if(!empty($result)) {
			foreach($result as $k => $row) {
				$result[$k]['reply'] = unserialize($row['reply']);
			}
		}
		return $result;
	}
	/**
	 * 查询个人最高的武功秘籍等级
	 * @param <int> $uin
	 */
	public static function getOneCheatsLv($uin){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_skill`,`app_kingdoms_dance_studio` WHERE `uin` = {$uin} AND app_kingdoms_skill.dance_studio_id = app_kingdoms_dance_studio.id ORDER BY lv DESC");
		return $result;
	}
	/**
	 * 查询最高的武功秘籍等级
	 */
	public static function getAllCheatsLv(){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM (SELECT * FROM `app_kingdoms_skill`,`app_kingdoms_dance_studio` WHERE app_kingdoms_skill.dance_studio_id = app_kingdoms_dance_studio.id  ORDER BY `lv` DESC) `temp`  GROUP BY uin ORDER BY `lv` DESC");
		return $result;
	}
	/**
	 * 查询救美最快榜
	 * @param <int> $type
	 */
	public static function getAllAtMost($type , $id){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM (SELECT * FROM `app_kingdoms_possess` WHERE `type` = '{$type}' AND `object_id` = '{$id}' ORDER BY `is_ap` DESC) `temp`  GROUP BY uin ORDER BY  `is_ap` DESC LIMIT 100");
		return $result;
	}
	/**
	 * 查询美女被救最多榜
	 * @param <int> $type
	 */
	public static function getAllLotOfBeauty($type){
		$Db = loadDB();
		$result = $Db->getAll("SELECT object_id,name,COUNT(object_id) AS count FROM app_kingdoms_possess,app_kingdoms_belle WHERE app_kingdoms_possess.object_id = app_kingdoms_belle.id AND  type = '{$type}' GROUP BY object_id HAVING count LIMIT 100");
		return $result;
	}
	/**
	 * 计算完成时间
	 * @param <int> $end_time
	 */
	public static function getEndDate($id , $uin , $end_time){
		$end_time = $end_time -time();
			
		if($end_time <= 0){
			self::getAlterState($id, $uin, 1);
		}else{
			$end_time_array = $end_time + (16 * 3600); // 因为我们时区是8
			$end_time_array = explode(':' , date('H:i:s' , $end_time_array));
			return $end_time_array;
		}
	}
	/**
	 * 修改状态
	 * @param <int> $uin
	 * @param <int> $id
	 * @param <int> $state
	 */
	public static function getAlterState($id , $uin , $state){
		$Db=loadDB();
		$update_data = array(
			'state' => $state,
		);
		$success = $Db->doUpdate('app_kingdoms_state' , $update_data , '`uin`= '.$uin.' AND `object_id` = ' . $id);
		return $success ? 0 : 774;
	}
	/**
	 * 放弃或完成删除状态
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function delState($uin , $id) {
		$Db = loadDB();
		
		$success = $Db->query("DELETE FROM `app_kingdoms_state` WHERE `uin` = {$uin} AND `id` = {$id}");
		
		return $success ? 0 : 775;
	}
	/**
	 * 查询武将
	 */
	public static function getAllGeneral(){
		$Db = loadDB();
		
		$success = $Db->getAll("SELECT * FROM `app_kingdoms_general`");
		
		return $success;
	}
	/**
	 * 查询武将详细资料
 	 * @param <int> $id
	 */
	public static function getOneGeneral($id){
		$Db = loadDB();
		
		$success = $Db->getOne("SELECT * FROM `app_kingdoms_general` WHERE `id` = {$id}");
		
		return $success;
	}
	/**
	 * 删除武将
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function delPossess($uin , $id) {
		$Db = loadDB();
		
		$success = $Db->query("DELETE FROM `app_kingdoms_possess` WHERE `uin` = {$uin} AND `id` = {$id}");
		
		return $success ? 0 : 775;
	}
	/**
	 * 查询物品栏
 	 * @param <int> $uin
	 * @param <int> $type
	 */
	public static function getAllPossess($uin , $type){
		$Db = loadDB();
		
		$success = $Db->getAll("SELECT * FROM `app_kingdoms_possess` WHERE `uin` = {$uin} AND `type` = '{$type}'");
		
		return $success;
	}
	/**
	 * 查询个人仓库武将资料,状态不唯一
 	 * @param <int> $uin
	 * @param <int> $type
	 */
	public static function getAllPossessGeneralMap($uin , $type){
		$Db = loadDB();
		
		$success = $Db->getAll("SELECT app_kingdoms_possess.id,app_kingdoms_possess.object_id,app_kingdoms_possess.state,app_kingdoms_general.name,app_kingdoms_general.ap,app_kingdoms_general.hp FROM `app_kingdoms_possess`,`app_kingdoms_general` WHERE `uin` = {$uin} AND `type` = '{$type}' AND app_kingdoms_possess.object_id = app_kingdoms_general.id");
		
		return $success;
	}
	/**
	 * 查询个人仓库武将资料
 	 * @param <int> $uin
	 * @param <int> $type
	 */
	public static function getAllPossessGeneral($uin , $type){
		$Db = loadDB();
		
		$success = $Db->getAll("SELECT app_kingdoms_possess.id,app_kingdoms_possess.object_id,app_kingdoms_possess.state,app_kingdoms_general.name,app_kingdoms_general.ap,app_kingdoms_general.hp FROM `app_kingdoms_possess`,`app_kingdoms_general` WHERE `uin` = {$uin} AND `type` = '{$type}' AND app_kingdoms_possess.object_id = app_kingdoms_general.id AND state = 1");
		
		return $success;
	}
	/**
	  *获取数量
	  *@param <int> $type
	  */
	 public static function getAllCount($type){
		$Db = loadDB();
		$count = $Db->getAll("SELECT uin,count FROM `app_kingdoms_count` WHERE `type` = '{$type}' ORDER BY count DESC");

		return $count;
	 }
	/**
	  *获取个人数量
	  *@param <int> $type
	  */
	 public static function getOneCount($uin , $type ,$insert = true ,$count){
		$Db = loadDB();
		$result = $Db->getOne("SELECT uin,count FROM `app_kingdoms_count` WHERE `uin` = '{$uin}' AND `type` = '{$type}'");
		// 如果数据不存在，则新写入一条数据
		if(empty($insert) && $result === false) {
			$query_data = array(
				'uin' => $uin,
				'type' => $type,
				'count' => $count,
				'time' => 0,
			);
			$Db->doInsert('app_kingdoms_count' , $query_data);
			
			$result = $query_data;
			
		}
		return $result;
	 }
	/**
	 * 修改个人数量
	 * @param <int> $uin
	 * @param <int> $type
	 * @param <int> $count
	 */
	public static function getAlterCount($uin , $type, $count){
		$Db=loadDB();
		$time = time();
		$update_data = array(
			'count' => $count,
			'time' => $time,
		);
		$success = $Db->doUpdate('app_kingdoms_count' , $update_data , "`uin` = {$uin} and `type` = '{$type}'");
		
		return $success ? 0 : 798;
	}
	/**
	 * 获取时间段数量栏
	 * @param <int> $uin
	 */
	public static function getCountTime($uin , $type , $first_time , $last_time){
		$Db = loadDB();
		$result= $Db->getAll("SELECT * FROM `app_kingdoms_count` WHERE (`time` BETWEEN $first_time and $last_time) AND `uin` = {$uin} AND `type` = '{$type}'");
		return $result;
	}
	/**
	 * 获取时间段物品栏
	 * @param <int> $uin
	 */
	public static function getPossessTime($uin , $type , $first_time , $last_time){
		$Db = loadDB();
		$result= $Db->getAll("SELECT * FROM `app_kingdoms_possess` WHERE (`end_time` BETWEEN $first_time and $last_time) AND `uin` = {$uin} AND `type` = '{$type}'");
		return $result;
	}
	/**
	 * 查询详细物品栏
 	 * @param <int> $uin
	 * @param <int> $type
	 * @param <int> $id
	 */
	public static function getOnePossess($uin , $id){
		$Db = loadDB();
		
		$success = $Db->getOne("SELECT * FROM `app_kingdoms_possess` WHERE `uin` = {$uin} AND `id` = {$id}");

		return $success;
	}
	/**
	 * 修改物品状态
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function getAlterPossess($uin , $id , $state , $is_ap){
	
		$Db=loadDB();
		$time = time();
		$update_data = array(
			'state' => $state,
			'end_time' => $time,
			'is_ap' => $is_ap,
		);
		
		$success = $Db->doUpdate('app_kingdoms_possess' , $update_data , '`uin`= '.$uin.' AND `id` = ' . $id);
		
		return $success ? 0 : 770;
	}
	/**
	 * 物品写入
	 */
	public static function getAddPossess($uin , $id , $type , $state , $is_ap){
		$Db = loadDB();
		$time = time();
	
		//把数据写入数组
		$query_data = array(
			'uin' => $uin,
			'object_id' => $id,
			'type' => $type,
			'time' => $time,
			'state' => $state,
			'end_time' => $time,
			'is_ap' => $is_ap,
		);
		$success = $Db->doInsert('app_kingdoms_possess' , $query_data);
		
		return $success ? 0 : 795;
	}
	/**
	 * 兵营数据初始化
	 * @param <int> $uin
	 * @param <boolean> $insert 是否在不存在时，新写入数据
	 */
	public static function getAddBarracks($uin , $insert = true){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_barracks` WHERE `uin` = " . $uin);
		
		// 如果数据不存在，则新写入一条数据
		if($insert && $result === false) {
			$query_data = array(
				'uin' => $uin,
				'lv' => 0,
				'time' => 0,
			);
			$Db->doInsert('app_kingdoms_barracks' , $query_data);
			
			$result = $query_data;
		}
		return $result;
	}
	/**
	 * 获取兵营等级可带领大将数量
	 * @param <int> $level
	 */
	public static function getBarracksGuide($level) {
		$guide = array();
		
		switch ($level) {
			case 0: $guide = array( 'staff' => 2 , 'upgrade' => 5 ); break;
			case 1: $guide = array( 'staff' => 3 , 'upgrade' => 8 ); break;
			case 2: $guide = array( 'staff' => 4 , 'upgrade' => 12 ); break;
			case 3: $guide = array( 'staff' => 5 , 'upgrade' => 18 ); break;
			case 4: $guide = array( 'staff' => 6 , 'upgrade' => 24 ); break;
			case 5: $guide = array( 'staff' => 7 , 'upgrade' => 30 ); break;
			case 6: $guide = array( 'staff' => 8 , 'upgrade' => 50 ); break;
			case 7: $guide = array( 'staff' => 9 , 'upgrade' => 100 ); break;
			case 8: $guide = array( 'staff' => 10 , 'upgrade' => -1 ); break;
		}
		return $guide;
	}
	/**
	 * 修改兵营等级
	 * @param <int> $uin
	 * @param <int> $id
	 * @param <int> $state
	 */
	public static function getAlterBarracks($uin , $lv){
		$Db=loadDB();
		$time = time();
		$update_data = array(
			'lv' => $lv,
			'time' => $time,
		);
		$success = $Db->doUpdate('app_kingdoms_barracks' , $update_data , '`uin`= '.$uin);
		return $success ? 0 : 794;
	}
	/**
	 * 查询三国美女表
	 */
	public static function getAllBelle(){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_belle`");
		return $result;
	}
	/**
	 * 查询三国美女详细资料
	 * @param <int> $id
	 */
	public static function getOneBelle($id){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_belle` WHERE `id` = {$id}");
		return $result;
	}
	/**
	 * 时间转换
	 */
	public static function getDate($time){
		$d = date('d' , $time);
		$m = date('m' , $time);
		$time = array('m' => $m ,'d' => $d);
		return $time;
	}
	/**
	 * 随机一条成人笑话表
	 * @param <int> $id
	 */
	public static function getOneRandJoke(){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_joke` ORDER BY RAND() DESC LIMIT 1");
		
		return $result;
	}
	/**
	 * 武功写入数据库
	 */
	public static function getAdddancestudio($lv , $ap , $name , $introduce , $required_time){
		$Db = loadDB();
		
		//把数据写入数组
		$insert_date = array(
			'lv' => $lv,
			'name' => $name,
			'ap' => $ap,
			'introduce' => $introduce,
			'required_time' => $required_time,
		);
		$success = $Db->doInsert('app_kingdoms_dance_studio' , $insert_date);
		return $success ? 0 : 779;
	}
	/**
	 * 排序 大到小
	 * @param unknown_type $arr
	 */
	public static function getArrSort($arr){
		//print_r($arr);
	   $i=0 ;
	   $j=0 ;
	   $temp=0 ;
	   $count = count($arr);
	   for($i=0; $i < $count; $i++){
	     for($j=$i; $j < $count; $j++){
	     	if($arr[$i]['count'] < $arr[$j]['count']){
		        $temp=$arr[$i];
		        $arr[$i]=$arr[$j];
		        $arr[$j]=$temp;
	      	}
	     }
	   }
	  return $arr;
	}
	/**
	 *查询用户的生命值
	 *$uid 用户的uin
	 */
	 public static function getUserHP($uin){
			 $Db=loadDB();
			 $result = $Db->getOne("SELECT * FROM `app_kingdoms_lady` WHERE `uin`=$uin");
			 
			 return $result;
	 }
	 /**
	 *查询今天与好友切磋的次数
	 *$now 之前时间
	 *$next 之后时间
	 */
	 public static function getDuelNum($now,$next){
			 $Db=loadDB();
			 $result = $Db->getAll("SELECT to_uin FROM `app_kingdoms_duel` WHERE `start_time` BETWEEN $now and $next");
			 
			 return $result;
	 }
	/**
	 *查询好友的方法
	 *$uin 用户的uin
	 */
	public static function getFriendInfo($uin) {
		
		$Db = loadDB();
		
		$result = $Db->getAll("SELECT `uin`,`name` FROM `user_baseinfo` WHERE uin in(SELECT `to_uin` FROM `friend` WHERE 
																					 
`uin` = $uin union all SELECT `uin` FROM `friend` WHERE `to_uin`= $uin)");
		
		return $result;
	}
	/**
	 *判断切磋的2个人是不是好友
	 *$uin 用户uin
	 *$to_uin  好友uin
	 */
	public static function judgeRelation($uin,$to_uin) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT `uin`,`name` FROM `user_baseinfo` WHERE uin in(SELECT `to_uin` FROM `friend` WHERE 
																					 
`uin` = $uin union all SELECT `uin` FROM `friend` WHERE `to_uin`= $uin);");

		return $result;
	}
	/**
	 *修改用户的生命值
	 *$uid 用户的uin
	 *$hp 用户修改后的生命值
	 */
	 public static function setUserHP($uin,$hp){
			 $Db=loadDB();
			 $result = $Db->getOne("UPDATE `app_kingdoms_lady` SET `hp` = $hp WHERE `uin` = $uin;");
			 
			 return $result;
	 }
	 /**
	 *增加与好友切磋的信息
	 *$uin 用户的uin
	 *$to_uin 好友的 uin
	 *$now_time 当前时间
	 */
	 public static function addDuelInfo($now_time,$time,$end_time,$uin,$to_uin){
		 $Db=loadDB();
		 $result = $Db->getOne("INSERT INTO `app_kingdoms_duel` (`uin`, `to_uin`,`start_time`,`time`,`end_time`,`state`) VALUES ('$uin', '$to_uin', '$now_time','$time','$end_time','2');");
 
		 return $result;
	 }
	 /**
	 *修改好友切磋的时间和状态
	 *$uin 用户的uin
	 *$to_uin 好友的 uin
	 *$now_time 当前时间
	 */
	 public static function setDuelTime($now_time,$uin,$to_uin ,$end_time){
		 $Db=loadDB();
		 $result = $Db->getOne("UPDATE `app_kingdoms_duel` SET `start_time` = $now_time , `state`=2,`end_time` = $end_time WHERE `uin` = $uin and `to_uin`= $to_uin");
 		 
		 return $result;
	 }
	 /**
	 *修改切磋完成后的状态
	 *$uin 用户的uin
	 *$to_uin 好友的 uin
	 */
	 public static function setDuelState($uin,$to_uin){
		 $Db=loadDB();
		 $result = $Db->getOne("UPDATE `app_kingdoms_duel` SET`state`=1 WHERE `uin` = $uin and `to_uin`= $to_uin");
 		 
		 return $result;
	 }
	 /**
	 *获取用户与好友的切磋状态
	 *$uin 用户的uin
	 *$to_uin 好友的 uin
	 */
	 public static function getDuelState($uin){
		 $Db=loadDB();
		 $result = $Db->getOne("SELECT * FROM `app_kingdoms_duel` WHERE `state` = '2' and `uin`=$uin;");
 		 
		 return $result;
	 }
	 
	 /**
	 *判断数据库中是不是第一次切磋
	 *$uin 用户uin
	 *$to_uin  好友uin
	 */
	public static function judgeDule($uin,$to_uin) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_duel` WHERE `uin`=$uin and `to_uin`=$to_uin;");

		return $result;
	}
	/**
	 *修改与好友切磋的状态
	 *$uin 用户uin
	 *$now 之前时间
	 *$next 之后时间
	 */
	public static function setDuleState($uin,$now,$next) {
		$Db = loadDB();
		 $result = $Db->getAll("UPDATE `app_kingdoms_duel` SET `state`='0' WHERE `uin` = $uin and `start_time` not between $now and $next");

		return $result;
	}
	/**
	 *查询好友的方法
	 *$uin 用户的uin
	 */
	public static function getDuelCount($uin) {
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_duel` WHERE state !='0'  and `uin`=$uin");
		
		return $result;
	}
	/**
	 *查询切磋结束时间的方法
	 *$uin 用户的uin
	 */
	public static function getDuelTime($uin) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_duel` WHERE state='2' and `uin`=$uin");
		
		return $result;
	}
	/**
	 *计算一天的时间 
	 */
	public static function getTime(){
		$time_now=time();
		$time_exp = date("d:H:i:s",$time_now);
		$time_arr = explode(":",$time_exp);
		$time = (($time_arr[1]*60+$time_arr[2])*60+$time_arr[3]);//今天00：00到当前过的秒数
		$now_time = $time_now-$time;//计算当天00:00
		$next_time= $time_now+3600*24-$time;//计算明天00:00
		$is_time = array($now_time,$next_time);
		return $is_time;
	}
	/**
	 * 地图绘制写入数据库
	 */
	public static function getAddMap($uin , $maps , $map_size , $arr_map){
		$Db = loadDB();
		$map = serialize($arr_map);
		$time = time();
		
		//把数据写入数组
		$query_data = array(
			'name' => $maps,
			'map_size' => $map_size,
			'map' => $map,
			'uin' => $uin,
			'time' => $time,
		);
		$success = $Db->doInsert('app_kingdoms_map' , $query_data);
		
		return $success ? 0 : 780;
	}
	/**
	 * 获取地图
	 */
	public static function getAllMap(){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_map`");
		
		return $result;
	}
	/**
	 * 获取单个地图
	 * @param <int> $id
	 */
	public static function getOneMap($id){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_map` WHERE `id` = {$id}");
		if(empty($result)) return array();
		$result['map'] = unserialize($result['map']);
		
		return $result;
	}
	/**
	 * 根据地图是否激活获取单个地图
	 * @param <int> $id
	 */
	public static function getOneMapState($id){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_map` WHERE `id` = {$id} AND `state` = 1");
		if(empty($result)) return array();
		$result['map'] = unserialize($result['map']);
		
		return $result;
	}
	/**
	 * 获取单个最新地图
	 * @param <int> $uin
	 */
	public static function getOneMapTime($uin){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_map` WHERE `uin` = {$uin} ORDER BY time DESC");
		if(empty($result)) return array();
		$result['map'] = unserialize($result['map']);
		
		return $result;
	}
	/**
	 * 修改地图信息
	 * @param $id
	 */
	public static function getAlterMap($id , $uin , $arr_map , $maps , $state , $belle_id){
		$Db=loadDB();
		$map = serialize($arr_map);
		$time = time();
		$update_data = array(
			'name' => $maps,
			'map' => $map,
			'uin' => $uin,
			'time' => $time,
			'state' => $state,
			'belle_id' => $belle_id,
		);
		$success = $Db->doUpdate('app_kingdoms_map' , $update_data , '`id`= '.$id);
		return $success ? 0 : 774;
	}
	/**
	 * 查询ID闯关地图大小
	 * @param <int> $id
	 */
	
	public static function getOneMixelWorlds($id){
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_kingdoms_mixelworlds` WHERE `belle_id` = " . $id);
		
		return $result;
	}
	
	/**
	 * 根据地图大小获取单个随机地图
	 * @param <int> $map_size
	 */
	public static function getAllMapSize($map_size){
		$Db = loadDB();
		$result = $Db->getAll("SELECT * FROM `app_kingdoms_map` WHERE `map_size` = '{$map_size}' AND `state` = 1");
		
		if(empty($result)) return array();
		if (!empty($result)){
			foreach($result as $k => $row) {
				$result[$k]['map'] = unserialize($row['map']);
			}
		}
		return $result;
	}
	/**
	 * 闯关地图写入状态表
	 * @param $uin
	 * @param $id
	 * @param $type
	 * @param $time
	 * @param $arr_map
	 */
	public static function getAddMapState($uin , $id , $type , $time , $arr_map){
		$Db = loadDB();
		$map = serialize($arr_map);
		$is_time = time();
		//把数据写入数组
		$insert_date = array(
			'uin' => $uin,
			'object_id' => $id,
			'reply' => $map,
			'time' => $is_time,
			'end_time' => $time,
			'type' => $type,
		);
		$success = $Db->doInsert('app_kingdoms_state' , $insert_date);
		return $success ? 0 : 790;
	}
	/**
	 * 修改reply状态内容
	 * @param <int> $uin
	 * @param <int> $id
	 * @param <int> $arr_map
	 */
	public static function getAlterStateMap($id , $uin , $arr_map){
		$Db=loadDB();
		
		$map = serialize($arr_map);
		$update_data = array(
			'reply' => $map,
		);
		$success = $Db->doUpdate('app_kingdoms_state' , $update_data , '`uin`= '.$uin.' AND `object_id` = ' . $id);

		return $success ? 0 : 774;
	}
	/**
	 * 方向坐标
	 * @param $x
	 * @param $y
	 * $up_y 上  $down_y 下  $left_x 左  $right_x 右
	 */
	public static function getDirection($x , $y){
		$up_y = $y - 1;
		$down_y = $y + 1;
		$left_x = $x - 1;
		$right_x = $x + 1;
		$direction = array('up_y' => $up_y, 'down_y' => $down_y, 'left_x' => $left_x, 'right_x' => $right_x);
		return $direction;
	}
	/**
	 * 地图大小给数字排序
	 */
	public static function getMapSize(){
		$map_size = array('6X6' => 1 , '6X9' => 2 , '9X9' => 3 , '9X16' => 4 , '16X16' => 5 , '16X32' => 6 , '32X32' => 7 , '32X48' => 8 , '48X48' => 9 , '64X64' => 10);
		return $map_size;
	}
	/**
	 * 武将攻击输赢
	 * @param $uin
	 * @param $ap
	 * @param $hp
	 * @param $is_ap
	 * @param $is_hp
	 */
	public static function getAttackGeneral($uin , $ap , $hp , $is_ap , $is_hp){
		$i = 1;
		$list = self::getDanceStudio();
		$count = count($list) - 1;
		$skill = self::getAllSkill($uin);
		
		$is_count = count($skill) - 1;
		
		while (1){
			if ($i == 1){
				if (empty($skill)){
					$studio[] = -1;
				}else{
					$studio[] = rand(0, $is_count);
				}
				
				$rand_ap = self::getRandAttack($ap);
				$is_hp -= $rand_ap;
				$studio_ap[] = $rand_ap;
				$i = 2;
			}else{
				$is_studio[] = rand(0, $count);
				$rand_is_ap= self::getRandAttack($is_ap);
				$hp -= $rand_is_ap;
				$is_studio_ap[] = $rand_is_ap;
				$i = 1;
			}
			if ($hp <= 0 || $is_hp <= 0){break;};
		}
		//print_r($studio);
		return $result=array('hp' => $hp , 'is_hp' => $is_hp , 'studio' => $studio , 'studio_ap' => $studio_ap , 'is_studio' => $is_studio , 'is_studio_ap' => $is_studio_ap , 'state' => 1);
	}
	/**
	 * 获得武将几率
	 */
	public static function get_Chance($percentage)
	{
	//检查$percentage是否在1~100之间
	if(($percentage>=1)&&($percentage<=100))
	{
	//取得1~100之间的乱数
	//(这里采用mt_rand而不是rand函数是因为PHP官网说mt_rand函数比rand函数有效率)
	$rand=mt_rand(1,100);
	
	//如果数字小于$percentage
	if($rand<=$percentage)
	{
	//成立
	$kk = 8;
	return true;
	}
	
	//不成立
	return false;
	}
	
	//数字并非在1~100之间
	return false;
	}
	/**
	 * 随机攻击力
	 * @param $ap
	 */
	public static function getRandAttack($ap){
		$min_ap = intval($ap / 3);
		$ap = rand($min_ap,$ap);
		//print_r($ap);
		return $ap;
	}
	/**
	 * 查询攻击段落的武将
	 * @param $min
	 * @param $max
	 */
	public static function getOneGeneralMinMax($min , $max){
		$Db = loadDB();
		
		$success = $Db->getOne("SELECT * FROM `app_kingdoms_general` WHERE ap BETWEEN $min AND $max ORDER BY RAND() DESC LIMIT 1");
		
		return $success;
	}
}
# end of script